package com.elong.spring.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ReflectTest {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Car initByDefaultConst() throws Throwable {
	ClassLoader loader = Thread.currentThread().getContextClassLoader();
	Class clazz = loader.loadClass("com.elong.common.spring.reflect.Car");
	
	/** 获取默认构造函数对象 **/
	Constructor cons = clazz.getDeclaredConstructor((Class[])null);
	Car car = (Car) cons.newInstance();
	
	//通过反射方法设置属性
	Method setBrand = clazz.getMethod("setBrand", String.class);
	setBrand.invoke(car, "红旗CZaa");
	Method setColor = clazz.getMethod("setColor", String.class);
	setColor.invoke(car, "黑色");
	Method setMaxSpeed = clazz.getMethod("setMaxSpeed", int.class);
	setMaxSpeed.invoke(car, 200);
	return car;
    }
    
    public static void main(String[] args) throws Throwable{
	Car car = initByDefaultConst();
	car.introduce();
    }
}
